<?php
namespace App\Http\Controllers\Api\Buz;

use App\Http\Controllers\Controller;
use App\Lib\BankMap;
use App\Models\BusinessScope;
use App\Models\ProvinceCity;
use App\Services\ApiResponseService;
use App\Validators\Buz\ImageValidator;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Ramsey\Uuid\Uuid;

class ToolController extends Controller
{
    public function getProvinceCity(Request $request)
    {
        $parent = $request->input('parent', 0);
        $result = ProvinceCity::where('parent', $parent)->orderBy('index', 'asc')->get(['id', 'name', 'parent']);
        $result = $result->makeHidden(['city', 'province', 'parent'])->toArray();
        return ApiResponseService::returnData($result);
    }

    public function getBankMap()
    {
        $banks = BankMap::keyValueSerialization();
        return ApiResponseService::returnData($banks);
    }

    public function getBuzScopes(Request $request)
    {
        $parent = $request->input('parent', 0);
        $result = BusinessScope::where('parent', $parent)->orderBy('index', 'asc')->get(['id', 'name']);
        return ApiResponseService::returnData($result);
    }

    public function uploadImg(Request $request, ImageValidator $imageValidator)
    {
        $imageValidator->with($request->all())->passesOrFail('upload');
        $image = $request->file('image');
        $image = Image::make($image->getRealPath())->resize(230, 170);
        $path = 'uploads/auth/'.Uuid::uuid1()->toString().'.jpg';
        $image->save($path);
        return ApiResponseService::returnData(compact('path'));

    }

}
